package com.put.mybills.database;

import java.util.ArrayList;
import java.util.List;
import com.put.mybills.model.Facture;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class FactureDataSource {
	
	  private static final String TAG = "FactureDataSource";
	
	  private SQLiteDatabase database;
	  private DatabaseHelper dbHelper;
	  

	  private String[] allColumns = { 	DatabaseHelper.FACTURE_ID,
			  							DatabaseHelper.FACTURE_NAME, 			  							
			  							DatabaseHelper.FACTURE_INTERVAL,
			  							DatabaseHelper.FACTURE_DESCRIPTION, 
			  							DatabaseHelper.FACTURE_ICON,
			  							DatabaseHelper.FACTURE_TERM,
			  							DatabaseHelper.FACTURE_EMAIL_NOTYFICATION,
			  							DatabaseHelper.FACTURE_POPUP_NOTYFICATION,
			  							DatabaseHelper.FACTURE_ICON_DRIVE_ID,
			  							DatabaseHelper.FACTURE_TEMPLAT_BARCODE_ID};
	  							
	
	  public FactureDataSource(Context context) {
	    dbHelper = new DatabaseHelper(context);
	  }
	
	  public void open() throws SQLException {
	    database = dbHelper.getWritableDatabase();
	  
	  }

	  public void close() {
	    dbHelper.close();
	  }
	  
	  
	  public void createFacture(Facture facture) {  
		  	
		  	Log.d(TAG, "Create facture function");
			ContentValues values = new ContentValues();
			
			//if (facture.getName() != null)
				values.put(DatabaseHelper.FACTURE_NAME, facture.getName());	
			
			//if (facture.getDescripiton() != null)
				values.put(DatabaseHelper.FACTURE_DESCRIPTION, facture.getDescripiton());	
			
			//if (facture.getIcon() != null)
				values.put(DatabaseHelper.FACTURE_ICON, facture.getIcon());	
			
			//if (facture.getInterval() != null)
				values.put(DatabaseHelper.FACTURE_INTERVAL, facture.getInterval());	
				
			//if (facture.getTerm() != null)	
				values.put(DatabaseHelper.FACTURE_TERM, facture.getTerm());	
				
				values.put(DatabaseHelper.FACTURE_EMAIL_NOTYFICATION, facture.getIsEmalNotyfication());
				
				values.put(DatabaseHelper.FACTURE_POPUP_NOTYFICATION, facture.getIsPopUpNotyfication());
				
				values.put(DatabaseHelper.FACTURE_ICON_DRIVE_ID, facture.getDriveIDIcon());
				
				values.put(DatabaseHelper.FACTURE_TEMPLAT_BARCODE_ID, facture.getTemplate_ID());
				
		
			long insertId = database.insert(DatabaseHelper.FACTURE_TABLE_NAME, null, values);
			
			Cursor cursor = database.query(DatabaseHelper.FACTURE_TABLE_NAME,
			    allColumns, DatabaseHelper.FACTURE_ID + " = " + insertId, null,
			    null, null, null);
			
			cursor.moveToFirst();
			
			//Facture newFacture = cursorToFacture(cursor);
			cursor.close();
				   
	  }
	  
	  public void modifyFactue(Facture facture ) {
		  
		  	Log.d(TAG, "Update facture function");
			ContentValues values = new ContentValues();
			
			//if (facture.getName() != null)
			values.put(DatabaseHelper.FACTURE_NAME, facture.getName());	
		
			//if (facture.getDescripiton() != null)
			values.put(DatabaseHelper.FACTURE_DESCRIPTION, facture.getDescripiton());	
		
			//if (facture.getIcon() != null)
			values.put(DatabaseHelper.FACTURE_ICON, facture.getIcon());	
		
			//if (facture.getInterval() != null)
			values.put(DatabaseHelper.FACTURE_INTERVAL, facture.getInterval());	
			
			//if (facture.getTerm() != null)	
			values.put(DatabaseHelper.FACTURE_TERM, facture.getTerm());	
			
			values.put(DatabaseHelper.FACTURE_EMAIL_NOTYFICATION, facture.getIsEmalNotyfication());
			
			values.put(DatabaseHelper.FACTURE_POPUP_NOTYFICATION, facture.getIsPopUpNotyfication());
			
			values.put(DatabaseHelper.FACTURE_ICON_DRIVE_ID, facture.getDriveIDIcon());
			
			values.put(DatabaseHelper.FACTURE_TEMPLAT_BARCODE_ID, facture.getTemplate_ID());
			
			String strFilter = DatabaseHelper.FACTURE_ID + "=" + facture.getId();
			database.update(DatabaseHelper.FACTURE_TABLE_NAME, values, strFilter,null);
				
	  }
	  
	  
	  public void deleteFacture(Facture facture) {
		    long id = facture.getId();
		    //System.out.println("Comment deleted with id: " + id);
		    database.delete(DatabaseHelper.FACTURE_TABLE_NAME, DatabaseHelper.FACTURE_ID + " = " + id, null);
	  }
	  
	  public void deleteAllFacture() {		  
		    //System.out.println("Factures deleted");
		    database.delete(DatabaseHelper.FACTURE_TABLE_NAME, null, null);
	  }
	  
	  public List<Facture> getAllFactures() {
		    List<Facture> factures = new ArrayList<Facture>();

		    Cursor cursor = database.query(DatabaseHelper.FACTURE_TABLE_NAME, allColumns, null, null, null, null, null);

		    cursor.moveToFirst();
		    while (!cursor.isAfterLast()) {
		    	Facture facture = cursorToFacture(cursor);
		    	factures.add(facture);
		      cursor.moveToNext();
		    }
		    // make sure to close the cursor
		    cursor.close();
		    return factures;
	  }
	  
	  
	
	  
	  public Facture getFacture(String factureName)
	  {
		  
		  Facture facture = null;
		  Cursor cursor = database.query(DatabaseHelper.FACTURE_TABLE_NAME, allColumns, null, null, null, null, null);

		  cursor.moveToFirst();
		  while (!cursor.isAfterLast()) {
			  Facture f = cursorToFacture(cursor);
		    	
			  if (f.getName().equals(factureName))
				  facture = f;
		    	
		      cursor.moveToNext();
		    }
		    cursor.close();
		    return facture;  
	  }

	  private Facture cursorToFacture(Cursor cursor) {
		  Facture facture = new Facture();		  
		  facture.setId(cursor.getInt(0));
		  facture.setName(cursor.getString(1));
		  facture.setInterval(cursor.getInt(2));
		  facture.setDescripiton(cursor.getString(3));
		  facture.setIcon(cursor.getString(4));
		  facture.setTerm(cursor.getString(5));
		  facture.setIsEmalNotyfication(cursor.getInt(6));
		  facture.setIsPopUpNotyfication(cursor.getInt(7));
		  facture.setDriveIDIcon(cursor.getString(8));
		  facture.setTemplate_ID(cursor.getInt(9));
		  return facture;
	  }	  

	  private String[] dataAllColumns = { 				  
				DatabaseHelper.DATA_NAME, 		  							
				DatabaseHelper.DATA_DRIVE};
	  
	  
	  public void insertDriveIDAppData(String dataName, String drivePath) {		
		  //Log.d(TAG, "Insert row for data table = " + dataName);
		  ContentValues values = new ContentValues();		
		  values.put(DatabaseHelper.DATA_NAME, dataName); 
		  values.put(DatabaseHelper.DATA_DRIVE, drivePath);
		  database.insert(DatabaseHelper.DATA_TABLE_NAME, null, values);	  
	  }
	  
	  public void updateDriveIDAppData(String dataName, String drivePath) {
			ContentValues values = new ContentValues();
			values.put(DatabaseHelper.DATA_DRIVE, drivePath);	
			Log.d(TAG, "Update row for data table = " + dataName + " with drive id = " + drivePath);
			String strFilter = DatabaseHelper.DATA_NAME + "='" + dataName + "'";
			database.update(DatabaseHelper.DATA_TABLE_NAME, values, strFilter, null);
	  }
	  
	  public String selectDriveIDAppData(String dataName) {		  
		  Cursor cursor = database.query(DatabaseHelper.DATA_TABLE_NAME, dataAllColumns, null, null, null, null, null);
		  cursor.moveToFirst();
		  String driveID = "null";
		  while (!cursor.isAfterLast()) {	
			  //Log.d(TAG, "Select data " + cursor.getString(0));
			  if (cursor.getString(0).equals(dataName)){
				  driveID = cursor.getString(1);
				  break;
			  }			 
		      cursor.moveToNext();
		    }
		  
		    cursor.close();
		    return driveID;
	  }	
	  
	  public void createDataRows() {
		  
		  String t = selectDriveIDAppData(DatabaseHelper.DATA_FACTURE);
		  //Log.d(TAG, "TEST FACTURE DATA " + t);
		  if (t == null || t.equals("null"))
			  insertDriveIDAppData(DatabaseHelper.DATA_FACTURE,"");
		  

		  String t1 = selectDriveIDAppData(DatabaseHelper.DATA_BILL);
		  //Log.d(TAG, "TEST BILL DATA " + t1);
		  if (t1 == null || t1.equals("null"))
			  insertDriveIDAppData(DatabaseHelper.DATA_BILL,"");		  
		  
		  
		  String t2 = selectDriveIDAppData(DatabaseHelper.DATA_UNITS);
		  //Log.d(TAG, "TEST UNIT DATA " + t2);
		  if (t2 == null || t2.equals("null"))
			  insertDriveIDAppData(DatabaseHelper.DATA_UNITS,"");
		  
	  }
}
